#include <QtGui/QMouseEvent>
#include <QtCore/QString>
#include "GLWidget.h"

	log4cxx::LoggerPtr
GLWidget::m_log(log4cxx::Logger::getLogger("GLWidget"));


GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) {
    setMouseTracking(true);
}

void GLWidget::initializeGL() 
{
	LOG4CXX_DEBUG(m_log, "initializeGL: ")
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_COLOR_MATERIAL);
    glEnable(GL_BLEND);
    glEnable(GL_POLYGON_SMOOTH);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glClearColor(0, 0, 0, 0);
}

void GLWidget::resizeGL(int w, int h) 
{
	LOG4CXX_DEBUG(m_log, "resizeGL: ")
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, w, 0, h); // set origin to bottom left corner
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void GLWidget::paintGL() 
{
	LOG4CXX_DEBUG(m_log, "paintGL: ")
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1,0,0);
    glBegin(GL_POLYGON);
    glVertex2f(0,0);
    glVertex2f(100,500);
    glVertex2f(500,100);
    glEnd();
}

void GLWidget::mousePressEvent(QMouseEvent *event) 
{
	LOG4CXX_DEBUG(m_log, "mousePressEvent: ")

}
void GLWidget::mouseMoveEvent(QMouseEvent *event) 
{
	QString str = "[" + QString::number(event->x())+ "," + QString::number(event->y()) + "]";
	std::cout << str.toStdString() << std::endl;
	LOG4CXX_DEBUG(m_log, "mouseMoveEvent: [x,y] = " << str.toStdString().c_str())
}

void GLWidget::keyPressEvent(QKeyEvent* event) 
{
    switch(event->key()) {
    case Qt::Key_Escape:
        close();
        break;
    default:
        event->ignore();
        break;
    }
}